class Mockolo < Formula
  desc "Efficient Mock Generator for Swift"
  homepage "https://github.com/uber/mockolo"
  url "https://github.com/uber/mockolo/archive/refs/tags/2.5.0.tar.gz"
  sha256 "af4f52f1c3146dea492997d6a5f99ab1c9bbc7687497a0149e63445a5c8d07d8"
  license "Apache-2.0"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_tahoe:   "2dbea87c99d506a39e39c6fdde0c470c569c0c9f5325ac134e0b9f3a4af03da4"
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "fcc6979486acd7c105de4f5c2eaaa957d74633e78d25dccbff3d1062f0fb3174"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "4614d2ea52e355d5d23288e3f18593632b67e49d1841527aeda36612cecbc9f1"
    sha256 cellar: :any_skip_relocation, sonoma:        "61a649c2863b7dcfe60f0faf6cebc89317db67dda45c9af4c16469235a925724"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "f0817392cbfb4d36ae20a446647ccc0902dec06b8ea4096c4d25e325dcb9b23a"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "76f2c928075ed61841c83d25a40e01e6b68b4ad71529eeed447f7c4ce5ffc1a6"
  end

  depends_on xcode: ["15.3", :build]

  uses_from_macos "swift" => :build

  def install
    args = if OS.mac?
      ["--disable-sandbox"]
    else
      ["--static-swift-stdlib"]
    end
    system "swift", "build", *args, "-c", "release", "--product", "mockolo"
    bin.install ".build/release/mockolo"
    generate_completions_from_executable(bin/"mockolo", "--generate-completion-script")
  end

  test do
    (testpath/"testfile.swift").write <<~SWIFT
      /// @mockable
      public protocol Foo {
          var num: Int { get set }
          func bar(arg: Float) -> String
      }
    SWIFT
    system bin/"mockolo", "-srcs", testpath/"testfile.swift", "-d", testpath/"GeneratedMocks.swift"
    assert_path_exists testpath/"GeneratedMocks.swift"
    output = <<~SWIFT.gsub(/\s+/, "").strip
      ///
      /// @Generated by Mockolo
      ///
      public class FooMock: Foo {
        public init() { }
        public init(num: Int = 0) {
            self.num = num
        }

        public private(set) var numSetCallCount = 0
        public var num: Int = 0 { didSet { numSetCallCount += 1 } }

        public private(set) var barCallCount = 0
        public var barHandler: ((Float) -> String)?
        public func bar(arg: Float) -> String {
            barCallCount += 1
            if let barHandler = barHandler {
                return barHandler(arg)
            }
            return ""
        }
      }
    SWIFT
    assert_equal output, shell_output("cat #{testpath/"GeneratedMocks.swift"}").gsub(/\s+/, "").strip
  end
end
